package useless;

import atom.UnaryAtom;
import formula.Formula;
import formula.UnaryFormula;
import literal.UnaryLiteral;
import proof.Proof;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class io extends JFrame{

    public io(){
        setTitle("三段论推理系统");
        setSize(500, 400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);//居中显示
        setLayout(new BorderLayout());//边界布局（5块）

        Container container=getContentPane();

        //JPanel inputPanel = new JPanel(new FlowLayout());//流布局，可是设置按键的左，中，右位置
//        JLabel jLabel=new JLabel("输入前提:");
//        container.add(jLabel);
//        setVisible(true);
        //add(inputPanel);
        ArrayList<Formula> conditions=new ArrayList<>();
        boolean flag=true;
        while (flag){
            String str=JOptionPane.showInputDialog("atom is:");
            if (!str.equals("exit")){
                if (str.length()==1){//只接收长度为1的unary atom
                    //System.out.println(str);
                    boolean negated=set_negated();//选择了yes，结果为true，negated；选择no，false，not negated
                    if (negated){//true,选择了yes，negated
                        UnaryFormula uf=new UnaryFormula(new UnaryLiteral(new UnaryAtom(str),true));
                        conditions.add(uf);
                    }else {//选择no，false，not negated
                        UnaryFormula uf=new UnaryFormula(new UnaryLiteral(new UnaryAtom(str),false));//有问题，negated的取值不确定，现在默认是false
                        conditions.add(uf);
                    }
                }
            }else {//输入exit，结束循环
                flag=false;
            }
        }
        for (Formula f:conditions) {
            System.out.println(f.toString());
        }
        //setVisible(true);
    }

    public boolean set_negated(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("weather negated");
        setBounds(100,100,240,180);
        getContentPane().setLayout(null);//绝对布局，全部位置由自己添加
        JLabel negated=new JLabel("weather negated:");
        negated.setBounds(20,20,120,22);
        getContentPane().add(negated);
        JRadioButton yes=new JRadioButton("YES");
        yes.setBounds(40,50,75,22);
        getContentPane().add(yes);
        JRadioButton no=new JRadioButton("NO");
        no.setBounds(120,50,75,22);
        no.setSelected(true);//设置no negated为默认选中状态
        getContentPane().add(no);
        ButtonGroup group=new ButtonGroup();// 组装单选按钮组
        group.add(yes);
        group.add(no);

        JButton button=new JButton("confirm");
        button.setBounds(60,80,85,22);
        add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("select yes:"+yes.isSelected());
                System.out.println("select no:"+no.isSelected());
                setVisible(false);
            }
        });

        return yes.isSelected();//选择了yes，结果为true，negated；选择no，false，not negated
    }


}

